package com.atnjupt.filter;

import com.atnjupt.utils.JDBCUtils;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author Wizarder
 * @create 2021-06-24 20:40
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            filterChain.doFilter(servletRequest,servletResponse);
            JDBCUtils.commitAndClose();//提交事物
        }  catch (Exception e) {
            JDBCUtils.rollBackAndClose();//回滚事物
            e.printStackTrace();
            throw  new RuntimeException(e);//产生一个异常对象给服务器
        }

    }

    @Override
    public void destroy() {

    }
}
